<?php
    // 8/29/2009: Immanuel192 rewrite this code

    /**
    * Change logs
    * Oct 2012 - sonhb - add profile module
    */


    class profilemodule {
        var $contact='';
        var $client;
        var $session='';
        var $login=false;
        var $config;
        
        function name() { return "Profile module"; }
        function description() { return "Manage CRM profile."; }
        function author() { return "sonhb - ABSoft.vn"; }
      
        function hasSources() { return true; }
        function hasContent() { return true; }
        function hasViews() { return true; }

        function supportsWorkflow() { return false; }

        function permissions($internal = "") {
            return array(
            // all permissions
            'administrate'=>'Administrate',
            // upload only, no delete , no meeting
            'upload'=>'Upload data',
            // delete + upload ; no meeting
            'delete'=>'Delete',
            // can do everything for only meeting; no upload ; can view
            'meeting'=>'Meeting Administrate',
            // can list meeting, can delete notes, can approve
            'meeting_delete'=>'Meeting Delete',
            // can list meeting, can write note, can not delete notes, can not approve, can write reply
            'meeting_assistant'=>'Meeting Assistant',
            );
        }

        function show($view,$loc = null, $title = "") {
            if(!exponent_users_isLoggedIn())
            {
                return exponent_http_error(403);
            }

            $template = new template('profilemodule',"main_page",$loc);

            $template->assign('moduletitle',$title);
            $template->assign('login',exponent_users_isLoggedIn()?1:0);
            $lang=exponent_lang_loadFile('modules/profilemodule/js_lang.php');           
            $template->assign('lang',json_encode($lang));
            $template->register_permissions(array('administrate','configure','create','edit','delete'),$loc);
            $template->output();
            exponent_sessions_set("AB_THEME_TOP_CONTAINER_SRC","profile_".LANG);
            exponent_sessions_set("AB_THEME_TOP_CONTAINER_TYPE","textmodule");

        }
        
        /**
        * Return default profile object
        * 
        */
        public static function _profile_init_default()
        {
            $obj=null;
            $obj->user_id=0;
            $obj->firstname="";
            $obj->lastname="";
            $obj->company_name="";
            $obj->tc_identity="";
            $obj->email="";
            $obj->website="";
            $obj->address="";
            $obj->tel1="";
            $obj->tel2="";
            $obj->skype="";
            $obj->fax="";
            $obj->tax_number="";
            $obj->tax_office="";
            $obj->country="";
            $obj->city="";
            $obj->postcode="";
            return $obj;
        }
        
        /**
        * Insert history for meeting
        * 
        */
        public static function _meeting_history_add($meeting_id, $content, $action){
            global $db;
            global $user;
            
            $history = null;
            $history->meeting_id = $meeting_id;
            $history->content = $content;
            $history->action = $action;
            $history->action_by = "{$user->firstname} {$user->lastname}({$user->username})";
            $history->date = time();
            $db->insertObject($history, "profilemodule_meeting_history");
        }
        
        function emptyDir() { 
          
            $path=BASE."/tmp/CRM_files/";
            global $config,$db;
            if((($config->cache*60)+($config->last_time))<time())
            {              
                if ($cachehandle = opendir($path)) {
                    while (false !== ($file = readdir($cachehandle))) {
                        if ($file != "." && $file != "..") {
                            $file2del = $path."/".$file;
                            unlink($file2del);     
                        }
                    }
                    $object = $db->selectObject('profilemodule_global_config',"1=1");
                    $object->last_time = time();
                    $db->updateObject($object,'profilemodule_global_config','1=1');
                    closedir($cachehandle);
                }
            }
            return true;
        }
        
        function downloadFile ($url, $path) {
            global $config;
            $destination = BASE."/tmp/CRM_files/$path";
            if (!file_exists($destination)) {    
                $ch = curl_init();
                $source = $url;
                curl_setopt($ch, CURLOPT_URL, $source);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_COOKIE,"PHPSESSID=".$config->session_id);
                $data = curl_exec ($ch);
                curl_close ($ch);


                $file = fopen($destination, "w+");
                fputs($file, $data);
                fclose($file);
                return $destination;
            }
            return $destination;
        }
        function SendMail($subject, $content, $format=0, $receive=array(), $dont_add_admin=false,$from_mail,$from){
            // send email to admin 
            $headers['From'] =  $from_mail ;
            $headers['Reply-to'] = $from_mail;

            require_once(BASE.'subsystems/mail.php');
            $mail = null;
            $mail = new exponentMail();

            if ($dont_add_admin==false)
                $mail->addTo($from_mail);
            if (!is_array($receive))
            {
                $receive=array($receive);
            }
            foreach ($receive as $v)
                $mail->addTo($v);

            $mail->subject($subject);
            if ($format == 0)
                $mail->addText($content);
            else
                $mail->addHTML($content);
            $mail->addHeaders($headers);
            $mail->addFrom($from_mail, $from );
            try{
                @$mail->send();
                return true;
            }
            catch (Exception $e){
                return false;
            }
        }
        
    }
    
    

?>
